package cn.maihe.elg.operation.centers.quanzhou.handler.service;

import cn.maihe.elg.operation.centers.epoint.config.EpointCenterInfosConfig;
import cn.maihe.elg.operation.centers.epoint.dto.EpointBaseRespDTO;
import cn.maihe.elg.operation.centers.epoint.handler.EpointCenterTradeService;
import cn.maihe.elg.operation.centers.quanzhou.dto.handler.QuanzhouClaimReqDTO;
import cn.maihe.elg.operation.centers.quanzhou.dto.handler.QuanzhouClaimRespDTO;
import cn.maihe.elg.operation.centers.supports.CenterTradeContext;
import cn.maihe.elg.operation.centers.supports.annotation.CenterTradeService;
import cn.maihe.elg.operation.model.enums.*;

import cn.maihe.elg.operation.repository.entity.AcceptOrderInfoDO;
import cn.maihe.elg.operation.service.order.AcceptOrderInfoService;
import cn.maihe.elg.operation.supports.system.ElgResourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * @Description 泉州中心理赔申请服务
 * @Author xxx
 * @Date 2025/08/31
 */
@CenterTradeService(
        provider = CenterProviderEnum.EPOINT,
        centerNo = CenterNoEnum.EPOINT_QUANZHOU,
        tradeType = TradeType.claimsnotice,
        reqClass = QuanzhouClaimReqDTO.class,
        respClass = EpointBaseRespDTO.class
)
@Slf4j
public class QuanzhouBaohanClaimService extends EpointCenterTradeService<QuanzhouClaimReqDTO, QuanzhouClaimRespDTO> {
    
    public QuanzhouBaohanClaimService(EpointCenterInfosConfig centerInfosConfig) {
        super(centerInfosConfig);
    }

    @Override
    protected void verifyParam(CenterTradeContext<QuanzhouClaimReqDTO, QuanzhouClaimRespDTO> context) {
        super.verifyParam(context);
        QuanzhouClaimReqDTO req = context.getReq();
        
        if (req == null) {
            throw new IllegalArgumentException("请求参数不能为空");
        }
        
        if (!StringUtils.hasText(req.getCnewguid())) {
            throw new IllegalArgumentException("业务流水号不能为空");
        }
        
        if (!StringUtils.hasText(req.getCpolicyno())) {
            throw new IllegalArgumentException("保函编号不能为空");
        }
        
        log.info("泉州中心理赔申请参数验证通过，业务流水号: {}", req.getCnewguid());
    }

    @Resource
    private AcceptOrderInfoService acceptOrderInfoService;
    
    @Resource
    private ElgResourceService elgResourceService;

    @Override
    public QuanzhouClaimRespDTO processService(CenterTradeContext<QuanzhouClaimReqDTO, QuanzhouClaimRespDTO> context) {
        AcceptOrderInfoDO acceptOrderInfoDO = buildAcceptOrderInfo(context);
        return buildResp(acceptOrderInfoDO);
    }

    private AcceptOrderInfoDO buildAcceptOrderInfo(CenterTradeContext<QuanzhouClaimReqDTO, QuanzhouClaimRespDTO> context) {
        QuanzhouClaimReqDTO req = context.getReq();
        
        // 根据保单号查询原始订单信息
        AcceptOrderInfoDO originalOrder = acceptOrderInfoService.getByGuaranteeNo(req.getCpolicyno());
        if (originalOrder == null) {
            throw new RuntimeException("未找到对应的保单信息: " + req.getCpolicyno());
        }
        
        // 更新订单状态为理赔申请中
        originalOrder.setStatus(ClaimsStatus.PROCESSING.getStatus());
        
        return acceptOrderInfoService.saveOrUpdateAcceptOrderInfo(originalOrder);
    }

    private QuanzhouClaimRespDTO buildResp(AcceptOrderInfoDO acceptOrderInfoDO) {
        QuanzhouClaimRespDTO resp = new QuanzhouClaimRespDTO();
        resp.setCode("1");
        resp.setMessage("理赔申请成功");
        
        // 构建理赔跳转链接，根据接口要求返回相关业务数据
        String claimUrl = elgResourceService.buildElgFrontUrl("/#/claimOrder?ordersn=" + acceptOrderInfoDO.getAcceptOrderNo());
        resp.setClaimurl(claimUrl);
        
        log.info("泉州中心理赔申请处理完成，保单号: {}, 跳转链接: {}", acceptOrderInfoDO.getGuaranteeNo(), claimUrl);
        return resp;
    }
}